package org.taotao.manager.web.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.taotao.commons.vo.DatagridResultVo;
import org.taotao.commons.vo.TaotaoResultVo;
import org.taotao.manager.pojo.Item;
import org.taotao.manager.service.ItemService;

import com.github.pagehelper.Page;

@Controller
@RequestMapping("/item")
public class ItemController {

	@Resource
	private ItemService itemService;
	
	//跳转到列表页面
	@RequestMapping("/item-list")
	public String toItemList(){
		return "item/item-list";
	}
	
	//查询商品
	@RequestMapping("/list")
	@ResponseBody
	public DatagridResultVo itemList(int page,int rows){
		DatagridResultVo datagridResultVo = new DatagridResultVo<>();
		Page<Item> items = itemService.getItemByPage(page, rows);
		
		datagridResultVo.setRows(items.getResult());
		datagridResultVo.setTotal(items.getTotal());
		return datagridResultVo;
	}
	
	//跳转到新增页面
		@RequestMapping("/item-add")
		public String toItemAdd(){
			return "item/item-add";
		}
		
		/**
		 * 跳到添加商品页面     item/save
		 * @return
		 */
		@RequestMapping("/save")
		@ResponseBody
		public TaotaoResultVo itemAdd(Item item){
			TaotaoResultVo taotaoResultVo = new TaotaoResultVo();
			Long id = itemService.saveItem(item);
			if (id != null && id > 0){
				// 添加成功
				//taotaoResultVo.ok();
				taotaoResultVo.setStatus(200);
				taotaoResultVo.setMsg("添加 成功");
			}else{
				// 添加失败
				taotaoResultVo.setStatus(-1);
				taotaoResultVo.setMsg("添加 失败");
			}
			return taotaoResultVo;
		}
		
		@RequestMapping("/delete")
		@ResponseBody
		public TaotaoResultVo delete(String ids){
			TaotaoResultVo taotaoResultVo = new TaotaoResultVo();
			itemService.delete(ids);
			taotaoResultVo.setStatus(200);
			return taotaoResultVo;
		}
}
